#include <irrlicht.h>
#include "demo.h"
#include "fx_starttexts.h"


using namespace irr;
using namespace irr::core;
using namespace irr::scene;


static ISceneManager *sSceneManager = 0;
static IMeshSceneNode *sCube = 0;


// Sync:

// 0, 263, 1063
// 3716, 4249, 5309

// same again n+8494... & 14330

// 16989


bool StartTextsFx::init()
{
    if (!gIrrDevice || !gIrrDevice->getSceneManager())
        return false;

    float cubeSize = 1;
    vector3df cameraPosition(0, 1.0f, -5.0f);

    sSceneManager = gIrrDevice->getSceneManager()->createNewSceneManager();

    ISceneManager *sceneManager = sSceneManager;

    sceneManager->addCameraSceneNode();
    sceneManager->getActiveCamera()->setPosition(cameraPosition);

    //sCube = sceneManager->addCubeSceneNode(cubeSize);
    //sCube->getMaterial(0).setFlag(video::EMF_LIGHTING, false);
    //sceneManager->getMeshManipulator()->setVertexColors(sCube->getMesh(), video::SColor(0xffffeedd));

    mInitialized = true;
    return true;
}


void StartTextsFx::render(double time, int timeMs)
{
    if (sCube)
    {
        vector3df eulerAngles(0, (float)gTimeMs / 10.0f, 0);
        sCube->setRotation(eulerAngles);

        const float endTransitionLength = (float)(mRenderEndTime - mFxEndTime);
        float fxEndTransitionScale = 1.0f - sBoxStep((float)time, (float)mFxEndTime, (float)mFxEndTime + endTransitionLength);
        sCube->setScale(vector3df(fxEndTransitionScale, fxEndTransitionScale, fxEndTransitionScale));
    }

	sSceneManager->drawAll();

    gT2Graphics->beginRender();

    gT2FontBig->setDrawMode(IDM_NORMAL);


    float fadeAway = 1.0f - sBoxStep((float)time, (float)mFxEndTime, (float)mFxEndTime + 1.0f);

    // 3716, 4249, 5309
    const char *txt = "";
    if (timeMs >= 3716) txt = "1 ";
    if (timeMs >= 4249) txt = "1 9 9";
    if (timeMs >= 5309) txt = "1 9 9 2";

    //gT2FontBig->drawString(gWidth / 2.0f, gHeight / 2.0f, "1 9 9 2", IF_ALIGN_CENTER, 1.0f, Color(1, 1, 1, 1.0f));
    gT2FontBig->drawString(gWidth / 2.0f, gHeight / 3.0f, txt, IF_ALIGN_CENTER, 1.0f, Color(1, 1, 1, fadeAway));

    gT2FontBig->setDrawMode(IDM_ADD);
    const int count = 8;
    for (int a = 0; a < count; ++a)
    {
        float scaleRef = 1.0f + fmodf((float)(time * 0.1 + (float)a / count), 1.0f);
        float fadeRef = 2.0f - scaleRef;
        float scale = scaleRef * scaleRef * scaleRef;
        float fade = (fadeRef / 5) * fadeAway;
        gT2FontBig->drawString(gWidth / 2.0f, gHeight / 3.0f, txt, IF_ALIGN_CENTER, scale, Color(1, 1, 1, fade));
    }


    char txt2[1024] = "";
    if (timeMs >= 8494) strcat(txt2, "tAAt\n");
    if (timeMs >= 8494 + 263) strcat(txt2, "is\n");
    if (timeMs >= 8494 + 1063) strcat(txt2, "born*\n");

    gT2FontBig->drawText(gWidth / 2.0f, floorf(gHeight * 0.5f), txt2, IF_ALIGN_TOP_HCENTER, 0.5f, 0, Color(1, 1, 1, fadeAway));

    if (timeMs >= 8494 + 3716)
        gT2FontBig->drawString(gWidth / 2.0f, floorf(gHeight * 0.95f), "* originally known as tArzAn tuotanto", IF_ALIGN_BOTTOM_HCENTER, 0.25f, Color(1, 1, 1, fadeAway));
// 0, 263, 1063
// 3716, 4249, 5309

// same again n+8494... & 14330


    gT2FontBig->setDrawMode(IDM_NORMAL);

    gT2Graphics->endRender();
}


void StartTextsFx::deinit()
{
    sSceneManager->drop();
    mInitialized = false;
}

